import express from 'express';
import {addKeyword, deleteKeyword, queryKeyword, queryKeywordAll} from "../controller/keyword.js";
import * as path from "path";

const router = express.Router();

router.get('/', (req, res) => {
    res.sendFile(path.join(process.cwd(), `src/html/addKeyword.html`));
});
router.post('/k', async (req, res) => {
    const {name, webHookUrl} = req.body;
    if (!name) {
        res.json({
            code: 4000,
            msg: '缺少参数name'
        })
        return
    }
    if (!webHookUrl) {
        res.json({
            code: 4000,
            msg: '缺少参数webHookUrl'
        })
        return
    }
    const result = await addKeyword({name, webHookUrl})

    res.json({
        code: 2000,
        msg: '添加成功',
        data: result
    });
});

router.get('/k', async (req, res) => {
   const result = await queryKeywordAll()
    res.json({
        code: 2000,
        msg: '查询成功',
        data: result
    })
});
router.delete('/k:id',async (req,res)=>{

    const {id} =req.params
    const result = await deleteKeyword(id)
    return res.json({
        code: 2000,
        msg: '删除成功',
        data: result
    })
})
export default router;
